"""
Case Type   : Operator
Case Name   : 操作符包含?、/
Create At   : 2024.10.8
Owner       : wang-tianjing1
Description :
    1、创建实现操作符的函数
    2、定义一个 / 操作符
    3、定义一个 ? 操作符
    4、定义10个字符的操作符
    5、定义超过10个字符以上的操作符
    6、包含 & | ` ?  字符以 + 结尾
    7、包含  ~ ! @ # % ^ & | ` ? 字符以 + 结尾
    8、包含 & | ` ?  字符以 - 结尾
    9、包含  ~ ! @ # % ^ & | ` ? 字符以 - 结尾
    10、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
    7、成功
    8、成功
    9、成功
    10、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class OpreatorTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('dbuser')
        self.user_node = Node('dbuser')
        self.constant = Constant()

    def test_dolphin(self):
        text = '----step1:创建实现操作符的函数；expect：创建成功----'
        self.log.info(text)
        sql_cmd = f'drop function if exists test_procedure() cascade;'\
                  f'create function test_procedure(a box,b box) return bool as\n'\
                  f'begin\n'\
                  f'if a = b then return t;\n'\
                  f'else return f;\n'\
                  f'end if;\n'\
                  f'end;\n'
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn('DROP FUNCTION', sql_res, '执行失败' + text)
        self.assertIn('CREATE FUNCTION', sql_res, '执行失败' + text)

        text = '----step2:定义一个 / 操作符;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator / (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'COMMUTATOR = /,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

        text = '----step3:定义一个 ? 操作符;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator ? (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'COMMUTATOR = ?,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

        text = '----step4:定义10个字符的操作符;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator ~!@#%^&|\\`? (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'commutator = ~!@#%^&|\\`?,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

        text = '----step5:定义超过10个字符以上的操作符;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator +-*/<>=~!@#%^&|\\`? (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'commutator = +-*/<>=~!@#%^&|\\`?,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

        text = '----step6:包含 & | ` ?  字符以 + 结尾;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator &|\\`?+ (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'commutator = &|\\`?+,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

        text = '----step7:包含  ~ ! @ # % ^ & | ` ? 字符以 + 结尾;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator ~!@#%^&|\\`?+ (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'commutator = ~!@#%^&|\\`?+,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

        text = '----step8:包含 & | ` ?  字符以 - 结尾;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator &|\\`?- (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'commutator = &|\\`?-,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

        text = '----step9:包含  ~ ! @ # % ^ & | ` ? 字符以 - 结尾;expect:成功----'
        self.log.info(text)
        sql_cmd1 = f'create operator ~!@#%^&|\\`?- (' \
                   f'leftarg = box,' \
                   f'rightarg = box,' \
                   f'commutator = ~!@#%^&|\\`?-,' \
                   f'procedure = test_procedure,' \
                   f'hashes, merges);'
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1)
        self.log.info(sql_res1)
        self.assertIn("CREATE OPERATOR", sql_res1, '执行失败' + text)

    def tearDown(self):
        text = '--step10:清理环境;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop function test_procedure() cascade;"
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn("DROP FUNCTION", sql_res, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
